package io;

import com.aliasi.xml.DelegateHandler;
import com.aliasi.xml.DelegatingHandler;
import com.aliasi.xml.SimpleElementHandler;
import com.aliasi.xml.TextAccumulatorHandler;

import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.aliasi.tracker.InputDocument;


public class AceDocumentHandler extends DelegatingHandler {


    Map<String,AceInputDocument> mId2InputDocument;

    DocEltHandler mDocEltHandler;
    public AceDocumentHandler(Map<String,AceInputDocument> id2InputDocument) {
        mId2InputDocument = id2InputDocument;
        mDocEltHandler = new DocEltHandler(this);
        setDelegate(DOC_ELT,mDocEltHandler);
    }

    public void finishDelegate(String qName, DefaultHandler handler) {
        if(qName.equals(DOC_ELT)) {
            //            System.out.println(mDocEltHandler.mId + " : " + mDocEltHandler.getTitleText());
            
            AceInputDocument doc = new AceInputDocument(mDocEltHandler.mId,
                                                        mDocEltHandler.mSource,
                                                        mDocEltHandler.mTitle,
                                                        mDocEltHandler.mText,
                                                        "");
            mId2InputDocument.put(mDocEltHandler.mId,doc);
        }
    }

    static String DOC_ELT = "DOC";
    static String DOCTYPE_ELT = "DOCTYPE";
    static String SOURCE_ATT = "SOURCE";
    static String DOCID_ELT = "DOCID";
    static String TEXT_ELT = "TEXT";
    static String HEADLINE_ELT = "HEADLINE";

    static class DocEltHandler extends DelegateHandler {
        String mId;
        String mTitle;
        String mText;
        String mSource;

        TextAccumulatorHandler mDocIdEltHandler = new TextAccumulatorHandler();
        TextAccumulatorHandler mHeadlineEltHandler = new TextAccumulatorHandler();
        TextAccumulatorHandler mTextEltHandler = new TextAccumulatorHandler();

        public void startElement(String nameSpace, String localName, String qName , Attributes atts) 
            throws SAXException {
            if (qName.equals(DOCTYPE_ELT)) {
                mId = null;
                mTitle = null;
                mText = null;
                mSource = null;
                mSource = atts.getValue(SOURCE_ATT);
            }
            super.startElement(nameSpace,localName,qName,atts);
        }

        public DocEltHandler(DelegatingHandler parent) {
            super(parent);
            setDelegate(DOCID_ELT,mDocIdEltHandler);
            setDelegate(HEADLINE_ELT,mHeadlineEltHandler);
            setDelegate(TEXT_ELT,mTextEltHandler);
        }

        public void finishDelegate(String qName, DefaultHandler handler) {
            if (qName.equals(HEADLINE_ELT)) {
                String id = mDocIdEltHandler.getText();
                mId = id.replaceAll(" ","");
            }
            if (qName.equals(HEADLINE_ELT)) {
                mTitle = mHeadlineEltHandler.getText();
            }
            else if (qName.equals(TEXT_ELT)) {
                mText = mTextEltHandler.getText();
            }
            super.finishDelegate(qName,handler);
        }

        public String getTitleText() {
            return mTitle + "\n" + mText;
        }
    }

}
